﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Net;
using HtmlAgilityPack;
public class ViNaAirlines : global::Airplane
{
    public void TimKiem()
    {
        WebClient client = new WebClient();
        string loai = initThamSo();

        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(new Uri(string.Format("https://cat.sabresonicweb.com/SSWVN/meridia?language=vi&posid=B3QE&page=requestAirMessage_air&action=airRequest&realrequestAir=realRequestAir&actionType=nonFlex&classService=CoachClass&currency=VND&depTime=0600&retTime=0600&direction={0}&departCity={1}&depDay={2}&depMonth={3}&temp_date=&returnCity={4}&retDay={5}&retMonth={6}&temp_date=&ADT=1&CHD=0&INF=0&submit=T%C3%ACm%20chuy%E1%BA%BFn%20bay&wpf_timed_action_0VNAirline_00215IBEFastTabV2_00215bookingYourTrip_00515vn_0051513bc15c0092_00515fd176_1=&ShowNote=Qu%C3%BD%20kh%C3%A1ch%20c%E1%BA%A7%20nh%E1%BA%AD%20%C4%91u7847%20y%20%C4%91u7911%20%20th%C3%B4ng%20tin%20%C4%91u7875%20%20l%C3%A0m%20th%E1%BB%A7t%E1%BB%A5%20!&wclang=VI&txtFamilyName=&txtMidName=&slDeparture=&txtConfirmCode=&txtFlightNumber=&rdoDay=on&", LoaiTK, DiemDi, NgayDi, ThangDi, DiemDen, NgayVe, ThangVe)));
    }

    private string initThamSo()
    {
        string loaitk = "";
        if (LoaiTK == "OneWay")
            LoaiTK = "onewaytravel";
        else
            LoaiTK = "returntravel";
        InitDiaDiem();
        InitThang();
        return loaitk;
    }

    private void InitThang()
    {
        switch (ThangDi)
        {
            case "2012-12":
                ThangDi = "DEC";
                break;
            case "2013-01":
                ThangDi = "JAN";
                break;
            case "2013-02":
                ThangDi = "FEB";
                break;
            case "2013-03":
                ThangDi = "MAR";
                break;
            case "2013-04":
                ThangDi = "APR";
                break;
            case "2013-05":
                ThangDi = "MAY";
                break;
            case "2013-06":
                ThangDi = "JUN";
                break;
            case "2013-07":
                ThangDi = "JUL";
                break;
            case "2013-08":
                ThangDi = "AUG";
                break;
            case "2013-09":
                ThangDi = "SEP";
                break;
            case "2013-10":
                ThangDi = "OCT";
                break;
            case "2013-11":
                ThangDi = "NOV";
                break;
        }
        switch (ThangVe)
        {
            case "2012-12":
                ThangVe = "DEC";
                break;
            case "2013-01":
                ThangVe = "JAN";
                break;
            case "2013-02":
                ThangVe = "FEB";
                break;
            case "2013-03":
                ThangVe = "MAR";
                break;
            case "2013-04":
                ThangVe = "APR";
                break;
            case "2013-05":
                ThangVe = "MAY";
                break;
            case "2013-06":
                ThangVe = "JUN";
                break;
            case "2013-07":
                ThangVe = "JUL";
                break;
            case "2013-08":
                ThangVe = "AUG";
                break;
            case "2013-09":
                ThangVe = "SEP";
                break;
            case "2013-10":
                ThangVe = "OCT";
                break;
            case "2013-11":
                ThangVe = "NOV";
                break;
        }
    }
    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        Document.LoadHtml(e.Result);
        HtmlAgilityPack.HtmlNode divnode = Document.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[1]/td[2]/div[1]/p[1]");
        int i = 1;
        while (divnode != null)
        {
            thongtincb info = new thongtincb();
            initCB(info, i);
            i++;
            string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[2]/div[1]/p[1]", i);
            divnode = Document.DocumentNode.SelectSingleNode(temp);
        }

        if (LoaiTK == "returntravel")
        {
            int j= 1;
            divnode = Document.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[1]/td[2]/div[1]/p[1]");
            while (divnode != null)
            {
                thongtincb info = new thongtincb();
                initKhuHoi(info, j);
                j++;
                string temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[2]/div[1]/p[1]", j);
                divnode = Document.DocumentNode.SelectSingleNode(temp);
            }
        }
        

    }

    private void initKhuHoi(thongtincb info, int j)
    {
        HtmlAgilityPack.HtmlNode divnode;
        string temp;

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[12]/span[1]/a[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[11]/span[1]/a[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[10]/span[1]/a[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[8]/span[1]/a[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[6]/span[1]/a[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        if (info.Giave == "")
        {
            return;
        }

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[2]/div[1]/p[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDi = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[3]/div[1]/p[1]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDen = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[2]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[4]/div[1]/span[3]", j);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Chuyenbay = divnode.InnerText.Trim();

        info.Hang = "Viet Nam AirLines";
        info.DiemDi = DiemDen;
        info.DiemDen = DiemDi;
        Airplane.lKhuHoi.Add(info);
    }

    private void initCB(thongtincb info, int i)
    {
        HtmlAgilityPack.HtmlNode divnode;
        string temp;

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[6]/span[1]/a[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[8]/span[1]/a[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[10]/span[1]/a[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[11]/span[1]/a[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[12]/span[1]/a[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        if (divnode != null) info.Giave = divnode.InnerText.Trim();

        if (info.Giave == "")
        {
            return;
        }

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[2]/div[1]/p[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDi = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[3]/div[1]/p[1]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.ThoiGianDen = divnode.InnerText.Trim();

        temp = string.Format("/html[1]/body[1]/div[2]/div[3]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[{0}]/td[4]/div[1]/span[3]", i);
        divnode = Document.DocumentNode.SelectSingleNode(temp);
        info.Chuyenbay = divnode.InnerText.Trim();

        info.Hang = "Viet Nam AirLines";
        info.DiemDi = DiemDi;
        info.DiemDen = DiemDen;
        Airplane.lInfo.Add(info);
    }
}
